package ar.com.coonocer.joy.mdg.util.templating;


import java.io.File;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ar.com.coonocer.joy.mdg.util.templating.gsp.GSPNameUtil;
import ar.com.coonocer.joy.mdg.util.templating.gsp.GSPTemplateUtil;
import ar.com.coonocer.joy.mdg.xmi.core.ModelElement;
import ar.com.coonocer.joy.mdg.xmi.model.Model;

public class TemplateUtil {

	private static Log logger = LogFactory.getLog(TemplateUtil.class);

	public static void applyTemplates(Model model, ModelElement umlObject, File templateFolder, File destinationFolder) {
		//
		List<File> templateFiles = TemplateFinder.getTemplates(umlObject, templateFolder);
		//
		for (File templateFile : templateFiles) {
			//
			File destinationFile = getDestinationFile(model, umlObject, templateFile, templateFolder, destinationFolder);
			//
			if (templateFile.isDirectory()) {
				//
				if (!destinationFile.exists()){
					logger.info("Creating folder " + templateFile + " for " + umlObject);
					destinationFile.mkdirs();
				}
				//
			} else if (templateFile.isFile()) {
				//
				logger.info("Aplying template " + templateFile + " on " + umlObject.getName());
				//
				GSPTemplateUtil.applyTemplate(model, umlObject, templateFolder, templateFile, destinationFile);
				//
			}
			//
		}
		//
	}

	public static File getDestinationFile(Model model, ModelElement umlObject, File templateFile, File templateFolder, File destinationFolder) {
		return GSPNameUtil.getDestinationFile(model, umlObject, templateFile, templateFolder, destinationFolder);
	}

}
